package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.Station;
import com.easy.mapper.StationMapper;
import com.easy.service.StationService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StationServiceImpl implements StationService {
    @Autowired
    StationMapper stationMapper;


    @Override
    @LogMethod(serviceName = "添加站点", recordParam = true, recordReturn = true)
    public Station add(Station station) {
        stationMapper.add(station);
        Station result = stationMapper.one(station.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "删除站点", recordParam = true, recordReturn = true)
    @CacheEvict(value = "station", key = "#p0" ,allEntries = true)//清空value制定的所有实例
    public int del(Integer id) {
        stationMapper.del(id);
        return 1;
    }

    @Override
    @LogMethod(serviceName = "修改站点", recordParam = true, recordReturn = true)
    @CachePut(value = "station", key = "#p0.id")
    public Station edit(Station station) {
        stationMapper.edit(station);
        return stationMapper.one(station.getId());
    }

    @Override
    @Cacheable(value = "station", key = "#p0")//也可以#p0表示第一个参数
    public Station one(Integer id) {
        return stationMapper.one(id);
    }

    @Override
    public List<Station> all(Station station) {
        return stationMapper.all(station);
    }

    @Override
    public void page(Page page, Station station) {
        int count = stationMapper.pageCount(station);
        Object data = stationMapper.page(page,station);
        page.setCount(count);
        page.setData(data);
    }
}
